package com.csdj.t309.geamtest.controller;


import com.csdj.t309.geamtest.common.CacheConstant;
import com.csdj.t309.geamtest.common.WebResult;
import com.csdj.t309.geamtest.common.WebResultMsgCode;
import com.csdj.t309.geamtest.entity.User;
import com.csdj.t309.geamtest.service.IUserService;
import com.csdj.t309.geamtest.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import java.util.Random;

/**
 * 发送邮件控制器
 */
@RestController
@RequestMapping("/mail")
public class MailController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private IUserService userService;
    @Resource
    private RedisService redisService;

    @Autowired
    private JavaMailSender mailSender;
    @RequestMapping("/send")
    public WebResult sendMail(@RequestParam(value = "email",required = false) String email) throws Exception{
        String randomCheckCode = getCheckCode();
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        WebResult webResult = new WebResult(WebResultMsgCode.SUCCESS);
        MimeMessageHelper mmh = new MimeMessageHelper(mimeMessage,true);
        mmh.setFrom("2379323670@qq.com");
        mmh.setTo(email);
        mmh.setSubject("欢迎注册拼图app");
        mmh.setText("<!DOCTYPE html>\n" +
                "<html>\n" +
                "\t<head>\n" +
                "\t\t<meta charset=\"utf-8\">\n" +
                "\t\t<title></title>\n" +
                "\t</head>\n" +
                "\t<body>\n" +
                "\t\t<p>校验码:</p>\n" +
                "\t\t<p>"+randomCheckCode+"</p>\n" +
                "\t\t<p>验证码的有效期为一小时</p>\n" +
                "\t\t<p>如非本人操作, 请忽略</p>\n" +
                "\t</body>\n" +
                "</html>",true);
        try {
            mailSender.send(mimeMessage);
            logger.info("测试邮件已发送。");
            redisService.setString("yianzhen",randomCheckCode,600);
            return webResult.putData("randomCheckCode",randomCheckCode);
        } catch (Exception e) {
            logger.error("发送邮件时发生异常了！", e);
            return webResult.putData("randomCheckCode",0);
        }
    }

    /**
     * 随机生成六位数字验证码
     * @return
     */
    public static String getCheckCode() {
        String ZiMu = "ABCDEFGabcdefg1234567890";
        String result = "";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            int index = random.nextInt(ZiMu.length());
            char c = ZiMu.charAt(index);
            result += c;
        }
        return result;

    }

    public static void main(String[] args) {
        System.out.println(getCheckCode());

    }

    @RequestMapping("/yaoqingma")
    public WebResult yaoQingMa(HttpServletRequest request){
        String yaoqing = userService.updeteyouqin(redisService.getObject(CacheConstant.HEADER_UID+request.getHeader(CacheConstant.HEADER_UID)));
        return new WebResult(WebResultMsgCode.SUCCESS).putData("yaoqing",yaoqing);
    }
}
